package peitong.peitong_oa.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import peitong.peitong_oa.dao.StaffDao;
import peitong.peitong_oa.domain.PageResult;
import peitong.peitong_oa.domain.QueryPageBean;
import peitong.peitong_oa.domain.pojo.Staff;
import peitong.peitong_oa.service.StaffService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffDao staffDao;
    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
         PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
         Page<Staff> page=(Page<Staff>) staffDao.findbycodeandname(queryPageBean.getQueryString());
         return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void upload(List<String[]> list) throws ParseException {
        ArrayList<Staff> stafflist = new ArrayList<>();
        for (String[] strings : list) {
            Staff staff = new Staff();
            staff.setGrade(0);
            staff.setCode(strings[1]);
            staff.setName(strings[2]);
            staff.setSex(strings[3].equals("男")?0:1);
            staff.setDepartment(strings[4]);
            staff.setPosition(strings[5]);
            staff.setIdcard(strings[6]);
            staff.setPhonenumber(strings[7]);
            staff.setAddress(strings[8]);
            staff.setCurrent_address(strings[9]);
            staff.setHiredate(new SimpleDateFormat("yyyy-MM-dd").parse(strings[10]));
            staff.setRemarks(strings[13]);
            staff.setFamily_phone(strings[14]);
            stafflist.add(staff);
        }
        staffDao.batchaddstaff(stafflist);
    }
    @Override
    public PageResult findpage_staff(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Staff> page=(Page<Staff>) staffDao.findstaffbycodeandname(queryPageBean.getQueryString(),queryPageBean.getDepartment());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void upload_staff(List<String[]> list) throws ParseException {
        ArrayList<Staff> stafflist = new ArrayList<>();
        for (String[] strings : list) {
            Staff staff = new Staff();
            staff.setGrade(1);
            staff.setCode(strings[1]);
            staff.setName(strings[2]);
            staff.setSex(strings[3].equals("男")?0:1);
            staff.setDepartment(strings[4]);
            staff.setPosition(strings[5]);
            staff.setIdcard(strings[6]);
            staff.setPhonenumber(strings[7]);
            staff.setAddress(strings[8]);
            staff.setCurrent_address(strings[9]);
            staff.setHiredate(new SimpleDateFormat("yyyy-MM-dd").parse(strings[10]));
            staff.setRemarks(strings[13]);
            staff.setFamily_phone(strings[14]);
            stafflist.add(staff);
        }
        staffDao.batchaddstaff(stafflist);
    }

    @Override
    public void addstaff(Staff staff) {
        staffDao.addstaff(staff);
    }

    @Override
    public void deletestaff(Integer id) {
        staffDao.deletebyid(id);
    }

    @Override
    public Staff findStaffDetail(Integer id) {
        return staffDao.findStaffDetail(id);
    }
}
